package com.it.thread_pool;

import java.util.concurrent.*;

public class ThreadPoolTest3 {
    public static void main(String[] args) throws Exception {
        // 1、通过ThreadPoolExecutor创建一个线程池对象
        ExecutorService pool = new ThreadPoolExecutor(3, 5, 8,
                TimeUnit.SECONDS, new ArrayBlockingQueue<>(4), Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());

        // 1-2 通过Executors创建一个线程池对象
        // Executors.newSingleThreadExecutor();

        // 新建固定数量线程池
        Executors.newFixedThreadPool(9);

        //核心线程数量到底配置多少呢？
        // 计算密集型任务（做一些计算由CPU进行计算）: 核心线程数量 = CPU的核数 + 1
        // IO密集型的任务（读取文件数据或者进行通信）: 核心线程数量 = CPU的核数 * 2


        // 2.使用线程池处理Callable任务
        Future<String> f1 = pool.submit(new MyCallable(100));
        Future<String> f2 = pool.submit(new MyCallable(200));
        Future<String> f3 = pool.submit(new MyCallable(300));
        Future<String> f4 = pool.submit(new MyCallable(400));

        System.out.println(f1.get());
        System.out.println(f2.get());
        System.out.println(f3.get());
        System.out.println(f4.get());
    }
}
